/*
 * pgp.c
 *	  各种实用功能。
 *
 * Copyright (c) 2005 Marko Kreen
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *	  notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *	  notice, this list of conditions and the following disclaimer in the
 *	  documentation和/或其他材料提供的发行版本中。
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * contrib/pgcrypto/pgp.c
 */

#include "postgres.h"

#include "pgp.h"
#include "px.h"

/*
 * 默认值。
 */
static int	def_cipher_algo = PGP_SYM_AES_128;
static int	def_s2k_cipher_algo = -1;
static int	def_s2k_mode = PGP_S2K_ISALTED;
static int	def_s2k_count = -1;
static int	def_s2k_digest_algo = PGP_DIGEST_SHA1;
static int	def_compress_algo = PGP_COMPR_NONE;
static int	def_compress_level = 6;
static int	def_disable_mdc = 0;
static int	def_use_sess_key = 0;
static int	def_text_mode = 0;
static int	def_unicode_mode = 0;
static int	def_convert_crlf = 0;

struct digest_info
{
	const char *name;
	int			code;
};

struct cipher_info
{
	const char *name;
	int			code;
	const char *int_name;
	int			key_len;
	int			block_len;
};

static const struct digest_info digest_list[] = {
	{"md5", PGP_DIGEST_MD5},
	{"sha1", PGP_DIGEST_SHA1},
	{"sha-1", PGP_DIGEST_SHA1},
	{"ripemd160", PGP_DIGEST_RIPEMD160},
	{"sha256", PGP_DIGEST_SHA256},
	{"sha384", PGP_DIGEST_SHA384},
	{"sha512", PGP_DIGEST_SHA512},
	{NULL, 0}
};

static const struct cipher_info cipher_list[] = {
	{"3des", PGP_SYM_DES3, "3des-ecb", 192 / 8, 64 / 8},
	{"cast5", PGP_SYM_CAST5, "cast5-ecb", 128 / 8, 64 / 8},
	{"bf", PGP_SYM_BLOWFISH, "bf-ecb", 128 / 8, 64 / 8},
	{"blowfish", PGP_SYM_BLOWFISH, "bf-ecb", 128 / 8, 64 / 8},
	{"aes", PGP_SYM_AES_128, "aes-ecb", 128 / 8, 128 / 8},
	{"aes128", PGP_SYM_AES_128, "aes-ecb", 128 / 8, 128 / 8},
	{"aes192", PGP_SYM_AES_192, "aes-ecb", 192 / 8, 128 / 8},
	{"aes256", PGP_SYM_AES_256, "aes-ecb", 256 / 8, 128 / 8},
	{"twofish", PGP_SYM_TWOFISH, "twofish-ecb", 256 / 8, 128 / 8},
	{NULL, 0, NULL}
};

static const struct cipher_info *
fc_get_cipher_info(int fc_code)
{
	const struct cipher_info *fc_i;

	for (fc_i = cipher_list; fc_i->name; fc_i++)
		if (fc_i->code == fc_code)
			return fc_i;
	return NULL;
}

int pgp_get_digest_code(const char *fc_name)
{
	const struct digest_info *fc_i;

	for (fc_i = digest_list; fc_i->name; fc_i++)
		if (pg_strcasecmp(fc_i->name, fc_name) == 0)
			return fc_i->code;
	return PXE_PGP_UNSUPPORTED_HASH;
}

int pgp_get_cipher_code(const char *fc_name)
{
	const struct cipher_info *fc_i;

	for (fc_i = cipher_list; fc_i->name; fc_i++)
		if (pg_strcasecmp(fc_i->name, fc_name) == 0)
			return fc_i->code;
	return PXE_PGP_UNSUPPORTED_CIPHER;
}

const char * pgp_get_digest_name(int fc_code)
{
	const struct digest_info *fc_i;

	for (fc_i = digest_list; fc_i->name; fc_i++)
		if (fc_i->code == fc_code)
			return fc_i->name;
	return NULL;
}

const char * pgp_get_cipher_name(int fc_code)
{
	const struct cipher_info *fc_i = fc_get_cipher_info(fc_code);

	if (fc_i != NULL)
		return fc_i->name;
	return NULL;
}

int pgp_get_cipher_key_size(int fc_code)
{
	const struct cipher_info *fc_i = fc_get_cipher_info(fc_code);

	if (fc_i != NULL)
		return fc_i->key_len;
	return 0;
}

int pgp_get_cipher_block_size(int fc_code)
{
	const struct cipher_info *fc_i = fc_get_cipher_info(fc_code);

	if (fc_i != NULL)
		return fc_i->block_len;
	return 0;
}

int pgp_load_cipher(int fc_code, PX_Cipher **fc_res)
{
	int			fc_err;
	const struct cipher_info *fc_i = fc_get_cipher_info(fc_code);

	if (fc_i == NULL)
		return PXE_PGP_CORRUPT_DATA;

	fc_err = px_find_cipher(fc_i->int_name, fc_res);
	if (fc_err == 0)
		return 0;

	return PXE_PGP_UNSUPPORTED_CIPHER;
}

int pgp_load_digest(int fc_code, PX_MD **fc_res)
{
	int			fc_err;
	const char *fc_name = pgp_get_digest_name(fc_code);

	if (fc_name == NULL)
		return PXE_PGP_CORRUPT_DATA;

	fc_err = px_find_digest(fc_name, fc_res);
	if (fc_err == 0)
		return 0;

	return PXE_PGP_UNSUPPORTED_HASH;
}

int pgp_init(PGP_Context **fc_ctx_p)
{
	PGP_Context *fc_ctx;

	fc_ctx = palloc0(sizeof *fc_ctx);

	fc_ctx->cipher_algo = def_cipher_algo;
	fc_ctx->s2k_cipher_algo = def_s2k_cipher_algo;
	fc_ctx->s2k_mode = def_s2k_mode;
	fc_ctx->s2k_count = def_s2k_count;
	fc_ctx->s2k_digest_algo = def_s2k_digest_algo;
	fc_ctx->compress_algo = def_compress_algo;
	fc_ctx->compress_level = def_compress_level;
	fc_ctx->disable_mdc = def_disable_mdc;
	fc_ctx->use_sess_key = def_use_sess_key;
	fc_ctx->unicode_mode = def_unicode_mode;
	fc_ctx->convert_crlf = def_convert_crlf;
	fc_ctx->text_mode = def_text_mode;

	*fc_ctx_p = fc_ctx;
	return 0;
}

int pgp_free(PGP_Context *fc_ctx)
{
	if (fc_ctx->pub_key)
		pgp_key_free(fc_ctx->pub_key);
	px_memset(fc_ctx, 0, sizeof *fc_ctx);
	pfree(fc_ctx);
	return 0;
}

int pgp_disable_mdc(PGP_Context *fc_ctx, int fc_disable)
{
	fc_ctx->disable_mdc = fc_disable ? 1 : 0;
	return 0;
}

int pgp_set_sess_key(PGP_Context *fc_ctx, int fc_use)
{
	fc_ctx->use_sess_key = fc_use ? 1 : 0;
	return 0;
}

int pgp_set_convert_crlf(PGP_Context *fc_ctx, int fc_doit)
{
	fc_ctx->convert_crlf = fc_doit ? 1 : 0;
	return 0;
}

int pgp_set_s2k_mode(PGP_Context *fc_ctx, int fc_mode)
{
	int			fc_err = PXE_OK;

	switch (fc_mode)
	{
		case PGP_S2K_SIMPLE:
		case PGP_S2K_SALTED:
		case PGP_S2K_ISALTED:
			fc_ctx->s2k_mode = fc_mode;
			break;
		default:
			fc_err = PXE_ARGUMENT_ERROR;
			break;
	}
	return fc_err;
}

int pgp_set_s2k_count(PGP_Context *fc_ctx, int fc_count)
{
	if (fc_ctx->s2k_mode == PGP_S2K_ISALTED && fc_count >= 1024 && fc_count <= 65011712)
	{
		fc_ctx->s2k_count = fc_count;
		return PXE_OK;
	}
	return PXE_ARGUMENT_ERROR;
}

int pgp_set_compress_algo(PGP_Context *fc_ctx, int fc_algo)
{
	switch (fc_algo)
	{
		case PGP_COMPR_NONE:
		case PGP_COMPR_ZIP:
		case PGP_COMPR_ZLIB:
		case PGP_COMPR_BZIP2:
			fc_ctx->compress_algo = fc_algo;
			return 0;
	}
	return PXE_ARGUMENT_ERROR;
}

int pgp_set_compress_level(PGP_Context *fc_ctx, int fc_level)
{
	if (fc_level >= 0 && fc_level <= 9)
	{
		fc_ctx->compress_level = fc_level;
		return 0;
	}
	return PXE_ARGUMENT_ERROR;
}

int pgp_set_text_mode(PGP_Context *fc_ctx, int fc_mode)
{
	fc_ctx->text_mode = fc_mode;
	return 0;
}

int pgp_set_cipher_algo(PGP_Context *fc_ctx, const char *fc_name)
{
	int			fc_code = pgp_get_cipher_code(fc_name);

	if (fc_code < 0)
		return fc_code;
	fc_ctx->cipher_algo = fc_code;
	return 0;
}

int pgp_set_s2k_cipher_algo(PGP_Context *fc_ctx, const char *fc_name)
{
	int			fc_code = pgp_get_cipher_code(fc_name);

	if (fc_code < 0)
		return fc_code;
	fc_ctx->s2k_cipher_algo = fc_code;
	return 0;
}

int pgp_set_s2k_digest_algo(PGP_Context *fc_ctx, const char *fc_name)
{
	int			fc_code = pgp_get_digest_code(fc_name);

	if (fc_code < 0)
		return fc_code;
	fc_ctx->s2k_digest_algo = fc_code;
	return 0;
}

int pgp_get_unicode_mode(PGP_Context *fc_ctx)
{
	return fc_ctx->unicode_mode;
}

int pgp_set_unicode_mode(PGP_Context *fc_ctx, int fc_mode)
{
	fc_ctx->unicode_mode = fc_mode ? 1 : 0;
	return 0;
}

int pgp_set_symkey(PGP_Context *fc_ctx, const uint8 *fc_key, int fc_len)
{
	if (fc_key == NULL || fc_len < 1)
		return PXE_ARGUMENT_ERROR;
	fc_ctx->sym_key = fc_key;
	fc_ctx->sym_key_len = fc_len;
	return 0;
}
